package com.corechina.apex_osswords.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.corechina.apex_osswords.common.APEXOSSWordsException;
import com.corechina.apex_osswords.common.HttpUtil;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.common.WeiXinUtil;
import com.corechina.apex_osswords.common.config.WeiXinConfig;
import com.corechina.apex_osswords.domain.entity.CoreVisitor;
import com.corechina.apex_osswords.domain.dao.CoreVisitorMapper;
import com.corechina.apex_osswords.service.ICoreVisitorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corechina.apex_osswords.service.ICoreWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 访客信息表 服务实现类
 * </p>
 *
 * @author cc
 * @since 2022-05-30
 */
@Service
public class CoreVisitorServiceImpl extends ServiceImpl<CoreVisitorMapper, CoreVisitor> implements ICoreVisitorService {

    @Autowired
    private CoreVisitorMapper coreVisitorMapper;

    @Autowired
    private ICoreWorkOrderService iCoreWorkOrderService;

    @Override
    public Response getPhone(String code, String openId) {
        QueryWrapper<CoreVisitor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("open_id", openId);
        CoreVisitor coreVisitorDb = coreVisitorMapper.selectOne(queryWrapper);
        if(coreVisitorDb != null){
            return Response.ok(coreVisitorDb);
        } else {
            if(code == null || code.trim().equals("")){
                return Response.error(2001,"未入库");
            }
        }

        Map<String, Object> param = new HashMap<>();
        param.put("appid", WeiXinConfig.APP_ID);
        param.put("secret", WeiXinConfig.APP_SECRET);
        param.put("grant_type", "client_credential");

        String accessToken = iCoreWorkOrderService.getAccessToken();

        Map<String, String> headMap = new HashMap<>();
        headMap.put("Content-type", "application/json");
        headMap.put("Accept", "application/json");
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("code", code);
        String wxPhoneResult = HttpUtil.postForJson(WeiXinConfig.WX_PHONE_URL + "?access_token=" + accessToken, paramMap, headMap);
        JSONObject jsonObject2 = JSONObject.parseObject(wxPhoneResult);
        if ((Integer) jsonObject2.get("errcode") != 0) {
            throw new APEXOSSWordsException("获取手机号失败");
        }
        JSONObject phoneJson = JSONObject.parseObject(jsonObject2.get("phone_info").toString());
        String phone = phoneJson.get("phoneNumber").toString();

        CoreVisitor coreVisitor = new CoreVisitor();
        coreVisitor.setCreateTime(LocalDateTime.now());
        coreVisitor.setOpenId(openId);
        coreVisitor.setTel(phone);
        coreVisitorMapper.insert(coreVisitor);
        return Response.ok(coreVisitor);
    }

}
